package com.powerdata.web.controller.paimon;

import com.powerdata.common.annotation.Log;
import com.powerdata.common.core.domain.AjaxResult;
import com.powerdata.common.enums.BusinessType;
import com.powerdata.system.domain.PaimonSyncTask;
import com.powerdata.system.domain.param.PaimonSyncTaskParam;
import com.powerdata.system.paimon.PaimonSyncTaskService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author deeprado
 * @version 1.0
 * @description
 * @date 2023/8/29 16:10
 */
@RestController
@RequestMapping("/paimon/syncTask")
public class SyncTaskController {

    @Resource
    private PaimonSyncTaskService paimonSyncTaskService;

    @Log(title = "获取入湖任务列表", businessType = BusinessType.QUERY)
    @ApiOperation("获取入湖任务列表")
    @PostMapping("/list")
    public AjaxResult taskList(@RequestBody PaimonSyncTaskParam paimonSyncTaskParam) {
        try {
            return AjaxResult.success(paimonSyncTaskService.taskList(paimonSyncTaskParam));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取入湖任务列表失败：" + e.getMessage());
        }
    }

    @Log(title = "创建并执行入湖任务", businessType = BusinessType.INSERT)
    @ApiOperation("创建并执行入湖任务")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PaimonSyncTask paimonSyncTask) {
        try {
            paimonSyncTaskService.add(paimonSyncTask);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("创建并执行入湖任务失败：" + e.getMessage());
        }
    }

    @Log(title = "修改并执行入湖任务", businessType = BusinessType.UPDATE)
    @ApiOperation("修改并执行入湖任务")
    @PostMapping("/modify")
    public AjaxResult modify(@RequestBody PaimonSyncTask PaimonSyncTask) {
        try {
            paimonSyncTaskService.add(PaimonSyncTask);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改并执行入湖任务失败：" + e.getMessage());
        }
    }

    @Log(title = "停止kafka的实时入湖任务", businessType = BusinessType.UPDATE)
    @ApiOperation("停止kafka的实时入湖任务")
    @PostMapping("/stopKafkaTask")
    public AjaxResult stopKafkaTask(@RequestBody PaimonSyncTask PaimonSyncTask) {
        try {
            paimonSyncTaskService.stopKafkaTask(PaimonSyncTask);
            return AjaxResult.success("停止成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("停止kafka的实时入湖任务失败：" + e.getMessage());
        }
    }

    @Log(title = "批量创建并执行Hive入湖任务", businessType = BusinessType.INSERT)
    @ApiOperation("批量创建并执行Hive入湖任务")
    @PostMapping("/batchAddHiveTask")
    public AjaxResult batchAddHiveTask(@RequestBody PaimonSyncTask paimonSyncTask) {
        try {
            paimonSyncTaskService.batchAddHiveTask(paimonSyncTask);
            return AjaxResult.success("批量创建并执行Hive入湖任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量创建并执行Hive入湖任务失败：" + e.getMessage());
        }
    }

    @Log(title = "批量创建并执行mysql入湖任务", businessType = BusinessType.INSERT)
    @ApiOperation("批量创建并执行mysql入湖任务")
    @PostMapping("/batchAddMySqlTask")
    public AjaxResult batchAddMySqlTask(@RequestBody PaimonSyncTask paimonSyncTask) {
        try {
            paimonSyncTaskService.batchAddMySqlTask(paimonSyncTask);
            return AjaxResult.success("批量创建并执行mysql入湖任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量创建并执行mysql入湖任务失败：" + e.getMessage());
        }
    }
}
